<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Portal\Controller;
use Common\Controller\HomeBaseController; 
/**
 * 首页
 */
class IndexController extends HomeBaseController {
   var $str='';
    //首页
	public function index(){
       $this->assign('select','1');
        if(IS_POST) {
            $data['title'] = I('post.title');
            $data['msg'] = I('post.msg');
            $data['phone'] = I('post.phone');
            $data['createtime'] = date('Y-m-d h:i:s',time());
            $data['status'] = 0;
            $flags = D('Guestbook')->add($data);
            if ($flags) {
                echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.'<script>alert("提交成功");</script>';
            } else {
                $this->error('提交失败');
            }
        }
        //置顶新闻
        $top=M('Posts as p')->join('LEFT JOIN sp_term_relationships AS tr ON p.id=tr.object_id')->where('tr.term_id=5 and p.istop=1')->limit(7)->order('post_date desc')->select();
        if($top){
            foreach ($top as $key => $val) {
                $top[$key]['smeta']=get_object_vars(json_decode($val['smeta']));
            }
        }
        $this->assign('top',$top);
        //新闻数据
        $new_info=M('Posts as p')->join('LEFT JOIN sp_term_relationships AS tr ON p.id=tr.object_id')->where('p.istop!=1 and tr.term_id=5')->limit(17)->order('post_date desc')->select();
        if($new_info){
            foreach ($new_info as $key => $val) {
                $new_info[$key]['smeta']=get_object_vars(json_decode($val['smeta']));
                $new_info[$key]['post_excerpt']=mb_substr($val['post_excerpt'],0,55,'utf-8');
            }
        }
        $this->assign('new_info',$new_info);
        //诊疗项目最新数据
        $female_id=$this->get_sort_id(6);
        $female_id.='6';
        $male_id=$this->get_sort_id(7);
        $male_id.='7';
        $ids=$female_id.','.$male_id;
        $Infertility_info=M('Posts as p')->join('LEFT JOIN sp_term_relationships AS tr ON p.id=tr.object_id')->where("p.istop!=1 and tr.term_id in ($ids)")->limit(13)->order('post_date desc')->select();
        if($Infertility_info){
            foreach ($Infertility_info as $key => $val) {
                $Infertility_info[$key]['post_excerpt']=mb_substr($val['post_excerpt'],0,55,'utf-8');
            }
        }
        $this->assign('Infertility_info',$Infertility_info);
        //男科置顶数据
        $male_info=M('Posts as p')->join('LEFT JOIN sp_term_relationships AS tr ON p.id=tr.object_id')->where("p.istop=1 and tr.term_id in ($male_id)")->limit(5)->order('post_date desc')->field('p.id,p.post_title,p.smeta')->select();
        if($male_info){
            foreach ($male_info as $key => $val) {
                $male_info[$key]['smeta']=get_object_vars(json_decode($val['smeta']));
            }
        }
        $this->assign('top_male_info',$male_info);
        //妇科置顶数据
        $female_info=M('Posts as p')->join('LEFT JOIN sp_term_relationships AS tr ON p.id=tr.object_id')->where("p.istop=1 and tr.term_id in ($female_id)")->limit(5)->order('post_date desc')->field('p.id,p.post_title,p.smeta')->select();
        if($female_info){
            foreach ($female_info as $key => $val) {
                $female_info[$key]['smeta']=get_object_vars(json_decode($val['smeta']));
            }
        }
        $this->assign('top_female_info',$female_info);
        //皮肤科置顶数据
        $pfk_info=M('Posts as p')->join('LEFT JOIN sp_term_relationships AS tr ON p.id=tr.object_id')->where('p.istop=1 and tr.term_id=31')->limit(5)->order('post_date desc')->field('p.id,p.post_title,p.smeta')->select();
        if($pfk_info){
            foreach ($pfk_info as $key => $val) {
                $pfk_info[$key]['smeta']=get_object_vars(json_decode($val['smeta']));
            }
        }
        $this->assign('pfk_info',$pfk_info);
//        //孕育技术置顶数据
//        $technology_info=$top=M('Posts as p')->join('LEFT JOIN sp_term_relationships AS tr ON p.id=tr.object_id')->where('tr.term_id=10 and p.istop=1')->limit(2)->order('post_date desc')->select();
//        if($technology_info){
//            foreach ($technology_info as $key => $val) {
//                $technology_info[$key]['smeta']=get_object_vars(json_decode($val['smeta']));
//            }
//        }
//        $this->assign('technology_info',$technology_info);
        //留言信息
        $message=D('Guestbook')->where('status=1')->limit(7)->order('createtime desc')->select();
        $this->assign('message',$message);
        //友链数据
        $frends_link=D('Links')->select();
        $this->assign('links',$frends_link);
        //医生列表
        $doc_list=D("Doc")->order("csort desc")->select();
        $this->assign("doc_list",$doc_list);
        
    	$this->display(":index");
    }   
    //
    //文章详情页
    public function detail(){
        $this->assign('select','5');
        $id=I('get.id','','intval');
        $info=M('Posts')->find($id);
        $info['post_date']=substr($info['post_date'],0,11);
        $this->assign('info',$info);
        //上一篇
        $topmap=array();
        $topmap['id']=array("GT",$id);
        $top_info=D("Posts")->where($topmap)->order("post_date desc,id desc")->field('post_title,id')->find();
        $this->assign("top_info",$top_info);
        //下一篇
        $nextmap=array();
        $nextmap['id']=array("LT",$id);
        $next_info=D("Posts")->where($nextmap)->order("post_date desc,id asc")->field('post_title,id')->find();
        $this->assign("next_info",$next_info);
        //浏览列表
        $term_id=D('TermRelationships')->where('object_id='.$id)->getField('term_id');
        $common_list=$top=M('Posts as p')->join('LEFT JOIN sp_term_relationships AS tr ON p.id=tr.object_id')->where("tr.term_id=$term_id")->limit(4)->order('post_date desc')->field('p.id,p.post_title')->select();
        $this->assign('common_list',$common_list);
        $this->display();
    }
    //获取分类id
    public function get_sort_id($pid){
        do{
            $flag=true;
            $data=M('Terms')->where('parent='.$pid)->field('term_id')->select();
            if($data){
                $res=$data;
                $id=M('Terms')->where('parent='.$pid)->getField('term_id');
                $this->get_sort_id($id);
            }
            $flag=false;
        }while($flag);
        foreach ($res as $key => $val) {
            $this->str.=$val['term_id'].',';
        }
        return $this->str;
    }
}


